package cn.yd.rb.service.serviceimpl;



import cn.yd.rb.mapper.CommentMapper;
import cn.yd.rb.mapper.NovelMapper;
import cn.yd.rb.po.Comment;
import cn.yd.rb.po.CommentExample;
import cn.yd.rb.po.Novel;
import cn.yd.rb.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private NovelMapper novelMapper;

    @Override
    public List<Comment> findCommentAll(Integer novel_id) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
       criteria.andNovelIdEqualTo(novel_id);
        List<Comment>  commentList = commentMapper.selectByExampleWithBLOBs(commentExample);
        return commentList;
    }

    @Override
    public void insertComment(Comment comment) {

        commentMapper.insertSelective(comment);
        //重新计算评论量
        Integer novel_id = comment.getNovelId();
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andNovelIdEqualTo(novel_id);
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        Integer counts = Integer.parseInt(String.valueOf(commentList.size()));
        Novel novel = novelMapper.selectByPrimaryKey(novel_id);
        novel.setCommentcounts(counts);
        novelMapper.updateByPrimaryKeySelective(novel);
    }
}

